package ihis.tms.server.redmine.model;

import ihis.core.IHISUtil;
import ihis.model.IHISConstants;
import ihis.tms.server.redmine.db.SqlMapClientManager;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.HashMap;

import com.ibatis.sqlmap.client.SqlMapClient;

public class TaskFilter extends ihis.model.TaskFilter {
	/**
	 * 
	 */
	private static final long serialVersionUID = -5984959082504203161L;
	
	public TaskFilter(ihis.model.TaskFilter oldFilter)
	{
/*		SortField sort;
		Boolean sortAscending;*/
		if(oldFilter != null)
		{
			this.setTaskId(oldFilter.getTaskId());
			this.setUserId(oldFilter.getUserId());
			this.setOwnerFirstName(oldFilter.getOwnerFirstName());
			this.setOwnerLastName(oldFilter.getOwnerLastName());
			this.setTeamId(oldFilter.getTeamId());
			this.setProjectId(oldFilter.getProjectId());
			this.setStatus(oldFilter.getStatus());
			this.setStartDate(oldFilter.getStartDate());
			this.setEndDate(oldFilter.getEndDate());
			this.setPeriodType(oldFilter.getPeriodType());
			this.setFromDueDateRange(oldFilter.getFromDueDateRange());
			this.setToDueDateRange(oldFilter.getToDueDateRange());

			this.setTaskType(oldFilter.getTaskType());
			this.setOwnerType(oldFilter.getOwnerType());
		}
	}
	
	public HashMap<String, Object> GetParameter() throws SQLException
	{
		HashMap<String, Object> result = new HashMap<String, Object>();

		SqlMapClient sqlMap = SqlMapClientManager.GetSqlMapClient();
		if(this.getTaskId() != null)
			result.put("id", this.getTaskId());
		if(this.getUserId() != null)
		{
			User user = (User) sqlMap.queryForObject("getUserByLogin", this.getUserId());
			result.put("owner", user.getId());
		}
		else if(this.getOwnerFirstName() != null && this.getOwnerLastName() != null)
		{

			HashMap<String, Object> hashMap = new HashMap<String, Object>();
			hashMap.put("firstname", this.getOwnerFirstName());
			hashMap.put("lastname", this.getOwnerLastName());
			User user = (User) sqlMap.queryForObject("getUserByName", hashMap);
			result.put("owner", user.getId());			
		}
		if(this.getProjectId() != null)
		{
			result.put("project", this.getProjectId().toString());
		}
		if(this.getStatus() != -1)
		{
			Integer[] statuses = null;
			switch(this.getStatus())
			{
				case IHISConstants.PROGRESS_STATUS_IN_PROGRESS:
					statuses = new Integer[4];
					statuses[0] = 1;
					statuses[1] = 2;
					statuses[2] = 4;
					statuses[3] = 6;
					break;
				case IHISConstants.PROGRESS_STATUS_COMPLETED:
					statuses = new Integer[2];
					statuses[0] = 3;
					statuses[1] = 5;
					break;
			}

			if(statuses != null) result.put("status", statuses);
		}
		if(!IHISUtil.isEmpty(this.getTeamId()))
		{
			result.put("team", Integer.parseInt(this.getTeamId()));
		}
		/*
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
		java.sql.Date hiredate = null;
		  
		hiredate = new java.sql.Date(sdf.parse(request.getParameter("hiredate")).getTime());
		emp.setHiredate(hiredate);
		[출처] iBatis 사용 예제 및 강좌|작성자 뻘맨
		*/
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
		if(this.getFromDueDateRange() != null){
			result.put("duedate_start", sdf.format(getFromDueDateRange()));
		}
		if(this.getToDueDateRange() != null){
			result.put("duedate_end", sdf.format(getToDueDateRange()));
		}
		if(this.getStartDate() != null){
			result.put("date_start", sdf.format(this.getStartDate()));
		}
		if(this.getEndDate() != null){
			result.put("date_end", sdf.format(this.getEndDate()));
		}
		return result;
	}
}
